package cn.flying.cloud.system.common.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.awt.image.BufferedImage;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.base.common.constants.Constants;
import cn.flying.cloud.base.common.utils.lang.CaptchaUtil;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.system.user.entity.SysUserInfo;
import cn.flying.cloud.system.user.service.SysUserInfoService;

/**
 * @description: 登录
 * @author: admin
 * @date: 2022年11月07日 22:12
 * @version: 1.0
 */
@Validated
@Controller
@RequestMapping("/auth/")
public class AuthController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysUserInfoService sysUserInfoService;

    /**
     * 登录页面
     *
     * @return
     */
    @RequestMapping("login/page")
    public String loginPage() {
        return "login";
    }

    /**
     * 注册页面
     *
     * @return
     */
    @RequestMapping("register/page")
    public String registerPage() {
        return "register";
    }

    @RequestMapping("captcha")
    @ResponseBody
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /*
             1.生成验证码
             2.把验证码上的文本存在session中
             3.把验证码图片发送给客户端
             */
        CaptchaUtil captchaUtil = new CaptchaUtil();
        BufferedImage image = captchaUtil.getImage();
        request.getSession().setAttribute(Constants.AUTH_CODE, captchaUtil.getText());
        captchaUtil.output(image, response.getOutputStream());
    }

    /**
     * 注册用户
     *
     * @return
     */
    @RequestMapping("register")
    @ResponseBody
    public Rt<?> register(SysUserInfo user) {
        // TODO
//        sysUserInfoService.register(user);
        return Rt.success("用户注册成功！");
    }

    /**
     * 忘记密码
     *
     * @return
     */
    @RequestMapping("forget/page")
    public String forget() {
        return "forget";
    }

    /**
     * 获取验证码
     *
     * @return
     */
    @RequestMapping("/auth/code")
    @ResponseBody
    public Rt<?> authCode(String phone) {
        //TODO 发送验证码
        System.out.println(phone);
        return Rt.success("验证码发送成功！");
    }

    /**
     * 校验验证码
     *
     * @return
     */
    @RequestMapping("/auth/valid/code")
    @ResponseBody
    public Rt<?> validCode(String cellphone, String vercode) {
        //TODO 校验验证码
        System.out.println(cellphone);
        System.out.println(vercode);
        return Rt.success("验证码校验通过！");
    }

    /**
     * 重置密码
     *
     * @return
     */
    @RequestMapping("/resetpass")
    @ResponseBody
    public Rt<?> resetpass(HttpServletRequest request) {
//        sysUserInfoService.resetUserPwd(request);
        return Rt.success("密码重置成功！");
    }


}
